package org.mzyyn.qr.controller;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import org.mzyyn.qr.constants.AppletsApiUrlMapping;
import org.mzyyn.qr.dto.prize.PrizeRecordListDTO;
import org.mzyyn.qr.dto.setting.SettingDTO;
import org.mzyyn.qr.dto.setting.SettingSaveDTO;
import org.mzyyn.qr.entity.SettingPO;
import org.mzyyn.qr.service.SettingService;
import org.qr.xiaocao.common.entity.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 配置表 前端控制器
 * </p>
 *
 * @author lipeng
 * @since 2024-09-05
 */
@RestController
public class SettingController {

    @Autowired
    SettingService settingService;

    /**
     * 成功助力
     * @return
     */
    @PostMapping(AppletsApiUrlMapping.SETTING_GET)
    public BaseResult settingGet(
            @RequestBody @Validated SettingDTO settingDTO
    ){

        return BaseResult.ok( settingService.getSetting(settingDTO));
    }

    @GetMapping(AppletsApiUrlMapping.SETTING_SPECIAL_GET)
    public String settingGet(@RequestParam("id") String id){
        SettingDTO settingDTO = new SettingDTO();
        settingDTO.setId(id);
        if(StrUtil.isEmpty(id)){
            return "id不能为空";
        }
        if(Integer.valueOf(id) > 100 || Integer.valueOf(id) < 1){
            return "id不能大于100";
        }
        SettingPO setting = settingService.getSetting(settingDTO);
        if( null == setting ){
            return "";
        }
        return setting.getContent();
    }

    @GetMapping(AppletsApiUrlMapping.SETTING_SPECIAL_SAVE)
    public String specialSave(
            @RequestParam("content") String content,
            @RequestParam("id") String id
    ){
        if(StrUtil.isEmpty(id)){
            return "id不能为空";
        }
        if(Integer.valueOf(id) > 100 || Integer.valueOf(id) < 1){
            return "id不能大于100";
        }
        SettingSaveDTO settingSaveDTO = new SettingSaveDTO();
        settingSaveDTO.setId(id);
        settingSaveDTO.setContent(content);
        settingSaveDTO.setTitle("特殊");
        settingService.saveSetting(settingSaveDTO);
        return "成功";
    }
}
